using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum TurnStateEnum
{
    //待输入,
    角色寻路,
    角色移动
}

public class Mouse : MonoBehaviour
{
    public Unit unit;
    public EnemyAi EnemyAi;
    Transform currentTarget;
    Cell currentCell;
    List<Vector2> currentPath;

    TurnStateEnum currentState;
    public static Mouse Instance;

    private void Start()
    {
        Instance = this;
    }

    void Update()
    {
        MouseDetect();
        MouseInput();
    }

    private void MouseInput()
    {
        if(Input.GetMouseButtonDown(0))
        {
            switch(currentState)
            {
                //case TurnStateEnum.待输入:
                //    if(currentCell!=null)
                //    {
                //        currentState = TurnStateEnum.角色寻路;
                //    }
                //    break;
                case TurnStateEnum.角色寻路 :
                    {
                        if (currentCell != null)
                        {
                            //unit.SetCell(currentCell);
                            unit.MoveToCell(currentPath);
                            currentState = TurnStateEnum.角色移动;
                        }
                    }
                    break;
                case TurnStateEnum.角色移动:
                    {

                    }
                    break;
            }
        }
    }
     
    public void SwitchState(TurnStateEnum state)
    {
        currentState = state;
    }

    private void MouseDetect()
    {
        Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hitInfo = Physics2D.Raycast(mouseRay.origin, mouseRay.direction);
        if (hitInfo.collider != null)
        {
            currentTarget = hitInfo.transform;

            Debug.Log("Hit object: " + currentTarget.name); // 打印出射线检测到的物体的名称

            Cell cell = currentTarget.GetComponent<Cell>();
            if (cell != null)
            {
                if (currentState == TurnStateEnum.角色寻路)
                {
                    if (currentCell != null && currentCell != cell)
                    {
                        currentCell.Normal();

                        PathCells(currentPath, "Normal");
                    }
                    currentCell = cell;
                    cell.High();
                    currentPath = EnemyAi.FindPath(unit.transform.position, cell.transform.position);
                    PathCells(currentPath,"High");
                //Debug.Log(cellList.Count);
                }
                else if (currentState == TurnStateEnum.角色移动)
                {
                    //PathCells("High");//一直在运行，需处理
                }
            }
            //else if(currentCell != null)
            //{
            //    currentCell.Normal();
            //    currentCell=null;

            //    PathCells("Normal");
            //    currentPath=null;
            //}
        }
    }
    public void PathCells(List<Vector2> path, string collor)
    {
        List<Cell> cellList = new List<Cell>();

        foreach (Vector2 position in path)
        {
            Vector2 pos = position + new Vector2(0.5f, 0.5f);
            Vector2 worldPosition = new Vector2(pos.x, pos.y);
            Collider2D collider = Physics2D.OverlapPoint(worldPosition, 1 << 3);

            if (collider != null)
            {
                GameObject obj = collider.gameObject;
                Cell cells = obj.GetComponent<Cell>();

                if (cells != null)
                {
                    cellList.Add(cells);
                    if(collor=="Normal")
                    {
                        Cell knightCell= unit.transform.GetComponent<Cell>();
                        cells.Normal();
                    }else if(collor=="High")
                    {
                        cells.High();
                    }
                }
            }
        }
    }
}
